package com.rongshu.view.userInfo.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.userInfo.adapter.TodayAdapter;
import com.rongshu.view.userInfo.entity.TodayRecord;
import com.rongshu.view.userInfo.iview.BrowsingView;
import com.rongshu.view.userInfo.presenter.BrowsingPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

import static com.rongshu.utils.UserInfoUtils.getUserId;

public class EarlierFragment extends BaseFragment<BrowsingPresenter> implements BrowsingView {
    @BindView(R.id.earlier_recyclerview)
    RecyclerView today_recyclerview;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.layout_empty)
    RelativeLayout layout_empty;

    //manager
    private LinearLayoutManager manager = null;
    //adapter
    private TodayAdapter mTodayAdapter = null;
    //map
    private Map<String, Object> map = null;
    private int pageIndex = 1;
    private int pageSize = 10;
    //refresh,load
    private boolean isrefresh = true;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_earlier_view;
    }

    @Override
    protected void initViews() {
        //set presenter
        setPresenter(new BrowsingPresenter(getActivity()), this);
        //layoutmanager
        manager = new LinearLayoutManager(getActivity());
        //adapter
        mTodayAdapter = new TodayAdapter(getActivity());
        //recyclerview
        today_recyclerview.setLayoutManager(manager);
        today_recyclerview.setAdapter(mTodayAdapter);
        //触发自动刷新
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                isrefresh = true;
                pageIndex = 1;
                refreshLayout.resetNoMoreData();
                getMyBrowsingHistory();
                refreshlayout.finishRefresh(2000, false);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                isrefresh = false;
                pageIndex++;
                getMyBrowsingHistory();
                refreshlayout.finishLoadmore(2000, false);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    /**
     * 我的浏览记录
     */
    private void getMyBrowsingHistory() {
        //user_id
        int user_id = getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("type", 2);
        map.put("page", pageIndex);
        map.put("pagesize", pageSize);
        presenter.getMyBrowsingHistory(user_id, 2,
                pageIndex, pageSize, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 我的浏览记录
     *
     * @param todayRecord
     */
    @Override
    public void onMyBrowsingHistory(TodayRecord todayRecord) {
        if (todayRecord.getErrCode() == 0) {
            List<TodayRecord.ResponseBean> list = todayRecord.getResponse();
            if (mTodayAdapter != null) {
                if (isrefresh) {
                    refreshLayout.finishRefresh();
                    if (list == null || list.isEmpty()) {
                        refreshLayout.setVisibility(View.GONE);
                        layout_empty.setVisibility(View.VISIBLE);
                    } else {
                        refreshLayout.setVisibility(View.VISIBLE);
                        layout_empty.setVisibility(View.GONE);
                        mTodayAdapter.setTodayRecords(list);
                        mTodayAdapter.notifyDataSetChanged();
                    }
                } else {
                    if (list == null || list.isEmpty()) {
                        refreshLayout.finishLoadmoreWithNoMoreData();
                    } else {
                        refreshLayout.finishLoadmore();
                        mTodayAdapter.addTodayRecords(list);
                        mTodayAdapter.notifyDataSetChanged();
                    }
                }
            }
        } else {
            toast(todayRecord.getErrMsg());
        }
    }
}
